import {_decorator, Color, Component, LabelComponent, Node, Sprite} from 'cc';

const {ccclass, property} = _decorator;

@ccclass('Item')
export class Item extends Component {
    @property(Sprite)
    itemSprite: Sprite = null!;

    @property(LabelComponent)
    value: LabelComponent = null!;


    public init(num: number) {
        this.value.string = num.toString();
        this.setColor(num);
    }


    private setColor(num: number) {
        let colors = {
            0: new Color(238, 228, 218),
            2: new Color(255, 179, 186),
            4: new Color(255, 223, 186),
            8: new Color(255, 255, 186),
            16: new Color(186, 255, 201),
            32: new Color(186, 225, 255),
            64: new Color(209, 186, 255),
            128: new Color(255, 186, 210),
            256: new Color(255, 186, 186),
            512: new Color(255, 223, 100),
            1024: new Color(255, 255, 100),
            2048: new Color(100, 255, 100),
        };
        let color = colors[num];
        if (color == null) {
            color = colors[2048];
            color = new Color(color.r - Math.log2(num) * 1.2, color.g - Math.log2(num) * 1.2, color.b - Math.log2(num) * 1.2)
        }
        this.itemSprite.color = color;
    }

    private setColorByRemaining(num: number, timeRemaining: number) {

    }
}


